#include "LevelChangeSubject.h"

#include <algorithm>

#include "Conversions.h"
#include "LevelChangeListener.h"
#include "Logger.h"

LevelChangeSubject::~LevelChangeSubject()
{
}

LevelChangeSubject& LevelChangeSubject::operator=(const LevelChangeSubject& rhs)
{
    if(this == &rhs) return *this;

    listeners = rhs.listeners;

    return *this;
}

void LevelChangeSubject::registerListener(LevelChangeListener* listener)
{
    if(std::find(listeners.begin(), listeners.end(), listener) == listeners.end())
    {
        listeners.push_back(listener);
    }
    else
    {
        using namespace Conversions;
        LOG("Warning: level change listener at " + toString(listener) +
            " already registered with subject at " + toString(this) + '.');
    }
}

void LevelChangeSubject::unregisterListener(LevelChangeListener* listener)
{
    listener_it it = std::find(listeners.begin(), listeners.end(), listener);
    if(it != listeners.end())
    {
        listeners.erase(it);
    }
}

void LevelChangeSubject::notifyListenersStarted()
{
    for(listener_it it = listeners.begin(); it != listeners.end(); ++it)
    {
        (*it)->onStarted();
    }
}

void LevelChangeSubject::notifyListenersIntermission(
bool isFirstLoaded, Level& level, const sf::Vector2f& entryPoint)
{
    for(listener_it it = listeners.begin(); it != listeners.end(); ++it)
    {
        (*it)->onIntermission(isFirstLoaded, level, entryPoint);
    }
}

void LevelChangeSubject::notifyListenersFinished()
{
    for(listener_it it = listeners.begin(); it != listeners.end(); ++it)
    {
        (*it)->onFinished();
    }
}